var prefix = "/reservationBrandCategory/manage";
$(function () {
    load();
});
$('#detailTable').on('load-success.bs.table', function (e, data) {
    if (data.total && !data.rows.length) {
        $('#detailTable').bootstrapTable('selectPage').bootstrapTable('refresh');
    }
});

function load() {
    $('#detailTable').bootstrapTable(
        {
            method: 'get', // 服务器数据的请求方式 get or post
            url: prefix + "/list", // 服务器数据的加载地址
            showRefresh: true,
            iconSize: 'outline',
            striped: true, // 设置为true会有隔行变色效果
            dataType: "json", // 服务器返回的数据类型
            pagination: true, // 设置为true会在底部显示分页条
            pageList: [10, 20, 50],
            singleSelect: false, // 设置为true将禁止多选
            pageSize: 10, // 如果设置了分页，每页数据条数
            pageNumber: 1, // 如果设置了分布，首页页码
            showColumns: true, // 是否显示内容下拉框（选择显示的列）
            sidePagination: "server", // 设置在哪里进行分页，可选值为"client" 或者
            queryParams: function (params) {
                return {
                    limit: params.limit,
                    offset: params.offset,
                    // order: 'desc',
                    // createTimeStart: $('#createTimeStart').val(),
                    // createTimeEnd: $('#createTimeEnd').val(),
                    queryItem: $.trim($("#queryItem").val()),
                    centreId: $("*[name='centreId']").val(),
                };
            },
            columns: [
                {
                    title: '序号',// 列标题
                    formatter: function (value, row, index) {
                        var pageSize = $('#detailTable').bootstrapTable('getOptions').pageSize;//通过表的#id 可以得到每页多少条
                        var pageNumber = $('#detailTable').bootstrapTable('getOptions').pageNumber;//通过表的#id 可以得到当前第几页
                        return pageSize * (pageNumber - 1) + index + 1;
                    }
                },
                {
                    field: 'centreName',
                    title: '商户名'
                },
                {
                    field: 'createTime',
                    title: '创建时间'
                },
                {
                    field: 'name',
                    title: '品牌名称'
                },
                {
                    field: 'logo',
                    title: '品牌Logo',
                    formatter: function (value, row, index) {
                        return '<img style="width:100px;height:80px" src=' + value + '>';
                    }
                },
                {
                    field: 'description',
                    title: '品牌背景图片',
                    formatter: function (value, row, index) {
                        return '<img style="width:100px;height:80px" src=' + value + '>';
                    }
                },
                {
                    field: 'sort',
                    title: '排序'
                },
                {
                    field: 'status',
                    title: '状态',
                    formatter: function (value, row, index) {
                            if (value == 0) {
                                return "<span class='btn btn-sm btn-success'>已删除</span>";
                            }
                            if (value == 1) {
                                return "<span class='btn btn-sm btn-info'>正常使用</span>";
                            }else {
                                return "<span class='label label-default'>状态异常</span>";
                            }
                    }
                },
                {
                    field: 'enabled',
                    title: '显示',
                    formatter: function (value, row, index) {
                        if (value == 0) {
                            return "<span class='label label-danger'>不显示</span>";
                        }
                        if (value == 1) {
                            return "<span class='label label-info'>显示</span>";
                        }else {
                            return "<span class='label label-default'>状态异常</span>";
                        }
                    }
                },
                {
                    title: '操作',
                    field: 'id',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var d = '';
                        var e = '';
                        var f = '';
                        var g = '';
                        var h = '';
                        var i = '';
                        var j = '';
                        var k = '';
                        if (row.status == 1) {
                            d = '<a class="btn btn-warning btn-sm '
                                + s_remove_h
                                + '" href="#" title="删除"  mce_href="#" onclick="remove(\'' + value + '\')"><i class="fa fa-remove"></i>删除</a> ';
                        }else{
                            d = '<a class="btn btn-warning btn-sm '
                                + s_remove_h
                                + '" href="#" title="恢复"  mce_href="#" onclick="start(\''
                                + value
                                + '\')"><i class="fa fa-remove"></i>恢复</a> ';
                        }

                        if (row.enabled == 1) {
                            e = '<a class="btn btn-warning btn-sm '
                                + s_remove_h
                                + '" href="#" title="隐藏"  mce_href="#" onclick="enabled(\'' + value + '\')"><i class="fa fa-remove"></i>隐藏</a> ';
                        }else{
                            e = '<a class="btn btn-warning btn-sm '
                                + s_remove_h
                                + '" href="#" title="显示"  mce_href="#" onclick="disEnabled(\'' + value + '\')"><i class="fa fa-remove"></i>显示</a> ';
                        }

                        f = '<a class="btn btn-primary btn-sm '
                            + s_edit_h
                            + '" href="#" mce_href="#" title="编辑" onclick="edit(\''
                            + value
                            + '\')"><i class="fa fa-edit"></i>编辑</a> ';

                        g = '<a class="btn btn-primary btn-sm '
                            + s_edit_h
                            + '" href="#" mce_href="#" title="详情" onclick="detail(\''
                            + value
                            + '\')"><i class="fa fa-edit"></i>详情</a> ';

                        h = '<a class="btn btn-primary btn-sm '
                            + s_edit_h
                            + '" href="#" mce_href="#" title="绑定分类" onclick="linkCategory(\''
                            + value
                            + '\')"><i class="fa fa-edit"></i>绑定分类</a> ';

                        i = '<a class="btn btn-primary btn-sm '
                            + s_edit_h
                            + '" href="#" mce_href="#" title="分类解绑" onclick="cancelLinkCategory(\''
                            + value
                            + '\')"><i class="fa fa-edit"></i>分类解绑</a> ';

                        j = '<a class="btn btn-primary btn-sm '
                            + s_edit_h
                            + '" href="#" mce_href="#" title="绑定商品" onclick="linkProductCentre(\''
                            + value
                            + '\')"><i class="fa fa-edit"></i>绑定商品</a> ';

                        k = '<a class="btn btn-primary btn-sm '
                            + s_edit_h
                            + '" href="#" mce_href="#" title="商品解绑" onclick="cancelLinkProductCentre(\''
                            + value
                            + '\')"><i class="fa fa-edit"></i>商品解绑</a> ';

                            return d + e + f + g + h + i + j + k;
                    }
                }]
        });
}

function reLoad() {
    $('#detailTable').bootstrapTable('refresh');
}


/**
 * 品牌详情页面
 */
function detail(id) {
    $.ajax({
        type: 'POST',
        dataType: 'JSON',
        url: prefix + "/getDetailByBrandId",
        data: {
            id: id
        },
        success: function (data) {
            if (data.code == 0) {
                var brandVo = data.brandVo;
                if (brandVo) {
                    layer.open({
                        id: 'brand_detail',
                        type: 2,
                        title: '品牌详情',
                        maxmin: true,
                        shadeClose: false,
                        area: ['80%', '90%'],
                        content: "/reservationBrandCategory/manage/detail/" + brandVo.id
                    });
                }
            }
        }
    });
}

/**
 * 绑定分类
 */
function linkCategory(id) {
    layer.open({
        id : "link_category",
        type : 2,
        title : "绑定分类",
        maxmin : true,
        shadeClose : true,
        area : [ '80%', '80%' ],
        content : "/reservationBrandCategory/manage/linkCategory?brandId="+id
    })
}

/**
 * 设置分类
 */
function setLinkCategory(brandId, pids) {
    $.ajax({
        url: "/reservationBrandCategory/manage/setLinkCategory",
        data: {"brandId": brandId, "pids": pids},
        dataType: "json",
        method:'POST',
        traditional:true,
        success: function (data) {
            reLoad();
        }
    })
}

/**
 * 分类解绑
 */
function cancelLinkCategory(id) {
    layer.open({
        id : "cancellink_category",
        type : 2,
        title : "分类解绑",
        maxmin : true,
        shadeClose : true,
        area : [ '80%', '80%' ],
        content : "/reservationBrandCategory/manage/cancelLinkCategory?brandId="+id
    })
}

/**
 * 设置分类解绑
 */
function setCancelLinkCategory(brandId, pids) {
    $.ajax({
        url: "/reservationBrandCategory/manage/setCancelLinkCategory",
        data: {"brandId": brandId, "pids": pids},
        dataType: "json",
        method:'POST',
        traditional:true,
        success: function (data) {
            reLoad();
        }
    })
}

/**
 * 绑定商品
 */
function linkProductCentre(id) {
    layer.open({
        id : "link_productCentre",
        type : 2,
        title : "绑定商品",
        maxmin : true,
        shadeClose : true,
        area : [ '80%', '80%' ],
        content : "/reservationBrandCategory/manage/linkProductCentre?brandId="+id
    })
}

/**
 * 设置绑定商品
 */
function setLinkProductCentre(brandId, pids) {
    $.ajax({
        url: "/reservationBrandCategory/manage/setLinkProductCentre",
        data: {"brandId": brandId, "pids": pids},
        dataType: "json",
        method:'POST',
        traditional:true,
        success: function (data) {
            reLoad();
        }
    })
}

/**
 * 商品解绑
 */
function cancelLinkProductCentre(id) {
    layer.open({
        id : "cancellink_productCentre",
        type : 2,
        title : "商品解绑",
        maxmin : true,
        shadeClose : true,
        area : [ '80%', '80%' ],
        content : "/reservationBrandCategory/manage/cancelLinkProductCentre?brandId="+id
    })
}

/**
 * 设置商品解绑
 */
function setCancelLinkProductCentre(brandId, pids) {
    $.ajax({
        url: "/reservationBrandCategory/manage/setCancelLinkProductCentre",
        data: {"brandId": brandId, "pids": pids},
        dataType: "json",
        method:'POST',
        traditional:true,
        success: function (data) {
            reLoad();
        }
    })
}

function add() {
    layer.open({
        type: 2,
        title: '添加品牌',
        maxmin: true,
        shadeClose: false, // 点击遮罩关闭层
        area : [ '100%', '100%' ],
        content: prefix + '/toAdd/' // iframe的url
    });
}
function remove(id) {
    layer.confirm('确定要禁用选中的记录？', {
        btn: ['确定', '取消']
    }, function () {
        $.ajax({
            url: prefix + "/remove",
            type: "post",
            data: {
                'id': id
            },
            success: function (data) {
                if (data.code == 0) {
                    layer.msg("禁用成功");
                    reLoad();
                } else {
                    layer.msg(data.msg);
                }
            }
        });
    })
}
function start(id) {
    layer.confirm('确定要启用选中的记录？', {
        btn: ['确定', '取消']
    }, function () {
        $.ajax({
            url: prefix + "/start",
            type: "post",
            data: {
                'id': id
            },
            success: function (data) {
                if (data.code == 0) {
                    layer.msg("启用成功");
                    reLoad();
                } else {
                    layer.msg(data.msg);
                }
            }
        });
    })
}

function enabled(id) {
    layer.confirm('确定要隐藏选中的记录？', {
        btn: ['确定', '取消']
    }, function () {
        $.ajax({
            url: prefix + "/enabled",
            type: "post",
            data: {
                'id': id,
                'enabled': 0
            },
            success: function (data) {
                if (data.code == 0) {
                    layer.msg("隐藏成功");
                    reLoad();
                } else {
                    layer.msg(data.msg);
                }
            }
        });
    })
}

function disEnabled(id) {
    layer.confirm('确定要显示选中的记录？', {
        btn: ['确定', '取消']
    }, function () {
        $.ajax({
            url: prefix + "/enabled",
            type: "post",
            data: {
                'id': id,
                'enabled': 1
            },
            success: function (data) {
                if (data.code == 0) {
                    layer.msg("显示成功");
                    reLoad();
                } else {
                    layer.msg(data.msg);
                }
            }
        });
    })
}

function edit(id) {
    layer.open({
        type: 2,
        title: '修改品牌',
        maxmin: true,
        shadeClose: false, // 点击遮罩关闭层
        area : [ '100%', '100%' ],
        content: prefix + '/edit/' + id // iframe的url
    });
}
